import { onlyGetFrom } from "@/util/net-disk/baidu/TokenInfo";

export async function GET(request: Request) {
  return onlyGetFrom((token) => {
    let listUrl = `https://pan.baidu.com/rest/2.0/xpan/multimedia?method=listall&access_token=${token.access_token}` +
    `&path=${encodeURIComponent("/ebook")}` +
    "&order=name" +
    "&recursion=1" +
    "&web=1";
    const reqUrl = new URL(request.url);
    const start = reqUrl.searchParams.get("start");
    if (start) {
      const numStart = parseInt(start);
      if (numStart > 0) {
        listUrl += `&start=${numStart}`;
      }
    }
    const limit = reqUrl.searchParams.get("limit");
    if (limit) {
      const numLimit = parseInt(limit);
      if (numLimit > 0) {
        // 百度官方建议不超过1000
        listUrl += `&limit=${Math.min(numLimit, 1000)}`;
      }
    }
    return listUrl;
  });
}